package common;

public class IntervalTimer 
{
	public IntervalTimer()
	{
		isSetTimer = false;
	}
	
	public boolean isSetTimer()
	{
		return isSetTimer;
	}
	
	public void SetIntervalTime(long interval)
	{
		tickInterval = interval;
	}
	
	public void beginTimer(long interval, long nowInMillis)
	{
		isSetTimer   = true;
		tickInterval = interval;
		tickOldTime  = nowInMillis;
	}
	
	private boolean countingTimer(long nowInMillis)
	{
		if (! isSetTimer)
		{
			assert(false);
			return false;
		}
		
		long newInMillis = nowInMillis;
		if (newInMillis - tickOldTime < tickInterval)
		{
			return false;
		}
		
		tickOldTime = newInMillis;
		
		return true;
	}
	
	public long getTimeLeft(long nowInMillis)
	{
		if (! countingTimer(nowInMillis))
		{
			return (tickInterval + tickOldTime - nowInMillis);
		}
		
		return 0;
	}
	
	private boolean isSetTimer;
	private long    tickInterval;
	private long    tickOldTime;
}
